package com.baibei.pay.recharge;

import android.content.Context;
import android.util.Log;

import com.baibei.basic.BasicPresenterImpl;
import com.baibei.ebec.sdk.ApiFactory;
import com.baibei.ebec.sdk.IUserApi;
import com.baibei.ebec.sdk.SuggestInFundInfo;
import com.baibei.model.BalanceInfo;
import com.baibei.model.FundTimeInfo;
import com.baibei.model.HelpContentInfo;
import com.baibei.model.PayModeInfo;
import com.baibei.model.RechargeAmountInfo;
import com.baibei.sdk.ApiDefaultObserver;
import com.baibei.sdk.RxObservable;
import com.blankj.utilcode.utils.LogUtils;
import com.rae.swift.Rx;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Action;
import io.reactivex.functions.Function;

import static android.content.ContentValues.TAG;

/**
 * 表现层
 * Created by shenminjie on 2017/8/15.
 * mail:shenminjie@100bei.com
 */

public class RechargePresenterImpl extends BasicPresenterImpl<RechargeContract.RechrageView> implements RechargeContract.Presenter {

    /**
     * 用户接口
     */
    private IUserApi mUserApi;

    public RechargePresenterImpl(Context context, RechargeContract.RechrageView view) {
        super(context, view);
        mUserApi = ApiFactory.getInstance().getUserApi();
    }

    @Override
    public void getBalance() {
        createObservable(mUserApi.getBalanceInfo()).subscribe(new ApiDefaultObserver<BalanceInfo>() {
            @Override
            protected void onError(String s) {
                if (mView != null) {
                    mView.onLoadBalanceFailed(s);
                }
            }

            @Override
            protected void accept(BalanceInfo balanceInfo) {
                if (mView != null) {
                    mView.onLoadBalance(balanceInfo.getBalance());
                }
            }
        });
    }

    @Override
    public void getRechargeInfo() {
        mView.showLoading();
        createObservable(mUserApi.getSuggestRechargeInfo()).flatMap(new Function<SuggestInFundInfo, Observable<List<PayModeInfo>>>() {
            @Override
            public Observable<List<PayModeInfo>> apply(@NonNull SuggestInFundInfo suggestInFundInfo) throws Exception {
                //加载建议金额
                List<RechargeAmountInfo> infos = new ArrayList<>();
                String[] numberArray = suggestInFundInfo.getFunds().split(",");
                for (int i = 0; i < numberArray.length; i++) {
                    infos.add(new RechargeAmountInfo(Rx.parseDouble(numberArray[i])));
                }
                if (mView != null) {
                    mView.onLoadRechargeAmount(infos);
                    mView.onLoadMinAmount(suggestInFundInfo.getMinFund());
                }
                return createObservable(mUserApi.getPayModeList(1));
            }
        }).flatMap(new Function<List<PayModeInfo>, Observable<FundTimeInfo>>() {
            @Override
            public Observable<FundTimeInfo> apply(@NonNull List<PayModeInfo> payModeInfos) throws Exception {
                //加载支付渠道
                if (mView != null) {
                    mView.onLoadPayModeList(payModeInfos);
                    Log.e(TAG, "apply:hh "+payModeInfos );
                }
                return createObservable(mUserApi.getFundTimeInfo("IN"));
            }
        }).doFinally(new Action() {
            @Override
            public void run() throws Exception {
                if (mView != null) {
                    mView.hideLoading();
                }
            }
        }).subscribe(new ApiDefaultObserver<FundTimeInfo>() {
            @Override
            protected void onError(String s) {
                if (mView != null) {
                    mView.onLoadBasicInfoFailed(s);
                }
            }

            @Override
            protected void accept(FundTimeInfo fundTimeInfo) {
                //加载时间
                if (mView != null) {
                    mView.onShowFundTimeInfo(fundTimeInfo.getInfo());
                }
            }
        });
    }

    @Override
    public void getHelp() {
        createObservable(mUserApi.getRechargeHelp()).subscribe(new ApiDefaultObserver<List<HelpContentInfo>>() {
            @Override
            protected void onError(String s) {

            }

            @Override
            protected void accept(List<HelpContentInfo> helpContentInfos) {
                mView.onLoadHelpSuccess(helpContentInfos.get(0).getText());
            }
        });
    }
}
